package main

import (
	"cn.pinming/gencode/core"
	"cn.pinming/gencode/modules"
	"github.com/kataras/iris"
	"github.com/kataras/iris/context"
	"github.com/kataras/iris/mvc"
	"time"
)

func main() {
	defer core.Close()
	app := core.GetApp()

	app.StaticWeb("/static", "./static")
	app.RegisterView(iris.HTML("./views", ".html").Reload(true))

	app.OnErrorCode(404, func(ctx context.Context) {
		fillContent(ctx)
		ctx.View("error404.html")
	})
	app.OnErrorCode(500, func(ctx context.Context) {
		fillContent(ctx)
		ctx.View("error500.html")
	})
	mvcApp := mvc.New(app)
	modules.InitRounter(mvcApp)
	core.Run()
}

func fillContent(ctx iris.Context) {
	sess := core.StartSession(ctx)
	flag, _ := sess.GetBoolean(core.NavCollapseKey)
	if core.GetAppConfig().Debug {
		ctx.ViewData("cssversion", time.Now().Unix())
	} else {
		ctx.ViewData("cssversion", "20170801")
	}
	ctx.ViewData("NavCollapse", flag)
	ctx.ViewData("user", core.CurrentUser(sess))
}
